package edu.chalmers;

import java.util.ArrayList;

public class SharedBookManager implements BookManager{
	private static SharedBookManager bookManager;
	private ArrayList<Book> books = new ArrayList<Book>();
	
	public static SharedBookManager getBookManager(){
		if(bookManager == null){
			bookManager = new SharedBookManager();
		}
		return bookManager;
	}
	
	private SharedBookManager(){
		books.add(new Book("JRR Tolkein", "Lord of the Rings", "123123", "Prototyping", 100));
		books.add(new Book("JK Rowling", "Harry Potter", "123123", "Methodology", 150));
		books.add(new Book("Jan Guillou", "Arn the Templar", "123123", "Physical Computing", 199));
		books.add(new Book("Kate Ross", "The Labyrinth", "132123", "Computer Graphics", 249));
		books.add(new Book("�sne Seierstad", "The Bookseller in Kabul", "123123", "Mobile Computing", 189));
	}
	
	public int count(){
		return books.size();
	}
	
	public Book bookAtIndex(int index){
		return books.get(index);
	}
	
	public Book createBook(){
		return new Book();
	}
	
	public Book createBook(String author, String title, String isbn, String course, int price){
		return new Book(author, title, isbn, course, price);
	}
	
	public ArrayList<Book> allBooks(){
		return books;
	}
	
	public void removeBook(Book b){
		books.remove(b);
	}
	
	public void moveBookAtIndex(int from, int to){
		Book temp = books.get(from);
		books.remove(from);
		books.add(to, temp);
	}
	
	public int minPrice(){
		int minPrice = Integer.MAX_VALUE;
		for(Book book : bookManager.allBooks()){
			minPrice = Math.min(minPrice, book.getPrice());
		}
		return minPrice;
	}
	
	public int maxPrice(){
		int maxPrice = 0;
		for(Book book : bookManager.allBooks()){
			maxPrice = Math.max(maxPrice, book.getPrice());
		}
		return maxPrice;
	}
	
	public float meanPrice(){
		return totalCost() / books.size();
	}
	
	public int totalCost(){
		int totalCostValue = 0;
		for(Book book : bookManager.allBooks()){
			totalCostValue += book.getPrice();
		}
		return totalCostValue;
	}
	
	public void saveChanges(){
		
		//TODO
	}
	
	public void loadBooks(ArrayList<Book> books){
		this.books = books;
	}
	
	public void addBook(Book book){
		books.add(book);
	}
	
	public void editBook(Book book, Book newBook){
		int index = books.indexOf(book);
		books.set(index, newBook);
	}
}
